import {Component, OnInit} from '@angular/core';
import {Router} from '@angular/router';
import {AddNewsService} from './news.service';
import {BaseComponent} from '../common/base.component';
import {AddNewsPage, AddNews} from './news';
import {FileUploader} from 'ng2-file-upload';
import {ParamConfig} from '../common/param.config';

declare var $: any;

@Component({
  selector: 'app-news',
  templateUrl: './news.component.html',
  styleUrls: ['./news.component.css']
})
export class NewsComponent extends BaseComponent implements OnInit {
  uploader: FileUploader = new FileUploader({
    url: ParamConfig.HTTP_WEB_HOST_URL + 'ofWeb/news/upload?type=0',
    method: 'POST',
    itemAlias: 'file',
    removeAfterUpload: true
  });
  editorTitle: String = '';
  isAddEditor: boolean;
  curPage: Number = 1;
  imgUrl: string;
  addNewsPage: AddNewsPage;
  addNews: AddNews[];
  curaddNews: AddNews;
  month: number;
  ifIndex: boolean;

  constructor(private addNewsService: AddNewsService, private router: Router) {
    super();
  }

  ngOnInit() {
    this.curaddNews = {
      newsId: '',
      newsTitle: '',
      newsCreatetime: '',
      newsUrl: '',
      newsMainpicurl: '',
      newsContent: '',
      newsIndexDisplay: '',
      iftop: false
    };
    this.queryList();
  }
// 上传图片
  selectedFileOnChanged(event: any) {
    console.log(event.target.value);
    // 这里是文件选择完成后的操作处理
    this.uploader.queue[0].onSuccess = (response, status, headers) => {
      // 上传文件成功
      if (status === 200) {
        // 上传文件后获取服务器返回的数据
        const tempRes = JSON.parse(response);
        console.log(tempRes);
        this.imgUrl = tempRes.systemFileTO.fileUrl;
        console.log(this.imgUrl);
      } else {
        // 上传文件后获取服务器返回的数据错误
      }
    };
    this.uploader.queue[0].upload(); // 开始上传
  }
// 查询新闻
  queryList() {
    this.addNewsService.getNews(this.curPage)
      .then(page => {
          this.addNewsPage = page;
          this.addNews = page.addNews.datas;
          console.log(this.addNews)
          for (const obj of this.addNews) {
            // this.ifIndex = obj.newsIndexDisplay === '0' ? true : false;
            if (obj.newsIndexDisplay === '1') {
              console.log(obj);
              const o = { ifIndex: false };
              const oo = Object.assign(obj, o);
              console.log(oo);
            }else {
              const oo = Object.assign(obj, { ifIndex: true });
            }
          }
        console.log(this.addNews);
        }
      );
  }

  // 新增新闻
  onAddNews() {
    this.isAddEditor = true;
    this.editorTitle = '新增新闻';
    this.curaddNews = {
      newsId: '',
      newsTitle: '',
      newsCreatetime: '',
      newsUrl: '',
      newsMainpicurl: '',
      newsContent: '',
      newsIndexDisplay: '',
      iftop: false
    };
    $('#data_editor').modal('show');
  }
// 取消新增/修改
  cancel() {
    this.curPage = 1;
    this.queryList();
  }
// 分页查询
  onPage(event) {
    this.curPage = event;
    this.queryList();
  }
// 显示删除弹框
  onDeleteNews(value: AddNews) {
    this.curaddNews = value;
    $('#delete_confirm').modal('show');  // 显示编辑对话框
  }
// 确认删除
  delete() {
    this.addNewsService.removeNewsinfo(this.curaddNews)
      .then(result => {
        if (result) {
          this.queryList();
          this.alert.info( '删除成功');
        } else {
          this.alert.error('删除失败');
        }
      });
    $('#delete_confirm').modal('hide');
  }
// 修改
  onEditNews(value: AddNews) {
    this.isAddEditor = false;
    this.editorTitle = '修改新闻';
    this.curaddNews = value;
    $('#ifttt').css('display', 'none')
    $('#data_editor').modal('show');  // 显示编辑对话框
  }

// 添加到首页
  onTopNews(value: AddNews) {
    this.curaddNews = value;
    this.addNewsService.goNewsIndex(this.curaddNews)
      .then(result => {
        if (result) {
          this.queryList();
          this.alert.info( '成功');
        } else {
          this.alert.error('失败');
        }
      });
  }



// 新增/修改
  save() {
    if (this.isAddEditor) { // 新增数据
      this.curPage = 1;
      this.addNewsService.addNewsInfo(this.curaddNews, this.imgUrl)
        .then(result => {
          if (result) {
            this.alert.info('新增成功');
            this.queryList();
          } else {
            this.alert.error('新增失败');
          }
        });
    } else { // 修改数据
      this.addNewsService.updateNewsinfo(this.curaddNews, this.imgUrl)
        .then(result => {
          if (result) {
            this.alert.info('修改成功');
            this.queryList();
          } else {
            this.alert.error('修改失败');
          }
        });
    }

    $('#data_editor').modal('hide');
  }
}
